<?php
/**
 *
 * @version $Id: CParameterList.php,v 1.2 2011-05-29 05:37:28 herve Exp $
 * @copyright 2011
 * @since 1.0
 */
class CParameterList implements ArrayAccess, IteratorAggregate, Countable {

	protected $_row;

	public function __construct($row= array ()) {
		$this->_row= $row;
	}

	// Called by isset()
	public function offsetExists($offset) {
		return isset ($this->_row[$offset]);
	}

	// Find value
	public function offsetGet($offset) {
		if (isset ($this->_row[$offset]))
			return $this->_row[$offset];
		else
			return null;
	}

	// Value update
	public function offsetSet($offset, $value) {
		if ($offset == null)
			return $this->_row[]= $value;
		else
			return $this->_row[$offset]= $value;
	}

	// If call to unset()
	public function offsetUnset($offset) {
		if (isset ($this->_row[$offset]))
			unset ($this->_row[$offset]);
	}

	public function getRow() {
		return $this->_row;
	}

	public function getKeys() {
		return array_keys($this->_row);
	}

	public function getIterator() {
		return new CParameterListIterator($this);
	}

	public function count() {
		return count($this->_row);
	}

	public function __clone() {
		$class= get_class($this);
		return new $class ($this->getRow());
	}

}

class CParameterListIterator implements Iterator {

	protected $_parameter= null;
	protected $_keys= null;
	protected $_currentIdx= -1;

	public function __construct($parameter) {
		$this->_parameter= $parameter;
		$this->_keys= $parameter->getKeys();
		$this->rewind();
	}

	public function current() {
		return ($this->_parameter[$this->key()]);
	}

	public function key() {
		return $this->_keys[$this->_currentIdx];
	}

	public function next() {
		$this->_currentIdx++;
	}

	public function rewind() {
		$this->_currentIdx= 0;
	}

	public function valid() {
		return ($this->_currentIdx >= 0 && $this->_currentIdx < count($this->_keys));
	}

}

?>
